package com.amos.think.config;

import lombok.extern.slf4j.Slf4j;
import org.springframework.boot.autoconfigure.mongo.MongoClientFactory;
import org.springframework.boot.autoconfigure.mongo.MongoClientSettingsBuilderCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.data.mongodb.MongoDatabaseFactory;
import org.springframework.data.mongodb.core.MongoTemplate;
import org.springframework.data.mongodb.core.SimpleMongoClientDatabaseFactory;
import org.springframework.data.mongodb.core.convert.MappingMongoConverter;
import org.springframework.data.mongodb.core.convert.MongoCustomConversions;
import org.springframework.data.mongodb.core.mapping.MongoMappingContext;

import java.util.concurrent.TimeUnit;

@Configuration
@Slf4j
public class MongoConfig {

    private int maxSize = 5;

    private int maxConnectionIdleTime =600;

    @Bean
    public MongoClientSettingsBuilderCustomizer initMongoClientSettingsBuilderCustomizer(){
        log.info("Mongo配置中...");
        return (builder) ->builder.applyToConnectionPoolSettings((connectionPoolSettingsBuilder)
                ->connectionPoolSettingsBuilder
                .maxSize(maxSize)
                .maxConnectionIdleTime(maxConnectionIdleTime, TimeUnit.SECONDS));
    }
    @Bean
    public MongoMappingContext mongoMappingContext() {
        return new MongoMappingContext();
    }

    @Bean
    public MongoTemplate mongoTemplate() throws Exception {
        MongoDatabaseFactory factory = new SimpleMongoClientDatabaseFactory("mongodb://admin:admin@47.103.33.108:27017/Users?authSource=admin");
        return new MongoTemplate(factory);
    }
}
